package com.heatingcharge.service.customer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heatingcharge.common.exception.BaseException;
import com.heatingcharge.common.util.BeanValidator;
import com.heatingcharge.common.util.ResponseCodeEnum;
import com.heatingcharge.dto.CustomerDTO;
import com.heatingcharge.entity.customer.Customer;
import com.heatingcharge.entity.customer.CustomerHouse;
import com.heatingcharge.mapper.customer.CustomerHouseMapper;
import com.heatingcharge.mapper.customer.CustomerMapper;
import com.heatingcharge.param.CustomerParam;
import com.heatingcharge.service.customer.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 客户资料  服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-07-12
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    CustomerHouseMapper customerHouseMapper;

    @Override
    public IPage<CustomerDTO> getCustomer(Page page, CustomerDTO customerDTO) {
        return baseMapper.selectCustomerDetail(page,customerDTO);
    }

    @Override
    public Integer save(CustomerParam customerParam) {
        //校验字段合法性
        BeanValidator.check(customerParam);
        //检验年度是否已经存在
        if (checkExist(customerParam.getIdNum(),null)){
            //年度已经存在
            throw new BaseException(ResponseCodeEnum.CUSTOMER_EXIST.getMessage(),ResponseCodeEnum.CUSTOMER_EXIST.getCode());
        }
        Customer customer=Customer.builder()
                .name(customerParam.getName())
                .phone(customerParam.getPhone())
                .otherPhone(customerParam.getOtherPhone())
                .idNum(customerParam.getIdNum())
                .build();
        baseMapper.insert(customer);
        CustomerHouse customerHouse=CustomerHouse.builder()
                .customerId(customer.getId())
                .houseId(customerParam.getHouseId())
                .build();

        return customerHouseMapper.insert(customerHouse);
    }
    private boolean checkExist(String idNum, Integer id) {
        QueryWrapper<Customer> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id_num",idNum);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        return baseMapper.selectOne(queryWrapper) !=null;
    }

    @Override
    public Integer update(CustomerParam customerParam) {
        //校验字段合法性
        BeanValidator.check(customerParam);
        //检验年度是否已经存在
        if (checkExist(customerParam.getIdNum(),customerParam.getId())){
            //年度已经存在
            throw new BaseException(ResponseCodeEnum.CUSTOMER_EXIST.getMessage(),ResponseCodeEnum.CUSTOMER_EXIST.getCode());
        }
//        HeatYear before=baseMapper.selectById(customerParam.getId());
//        //待更新的不存在
//        if (ObjectUtil.isNull(before)){
//            throw new BaseException(ResponseCodeEnum.CUSTOMER_NON_EXIST.getMessage(),ResponseCodeEnum.CUSTOMER_NON_EXIST.getCode());
//        }
        Customer after =new Customer();
        BeanUtils.copyProperties(customerParam,after);
        return baseMapper.updateById(after);
    }

    @Override
    public Integer delete(CustomerParam customerParam) {
        return baseMapper.deleteById(customerParam.getId());
    }
}
